Reference
  Area\Util.txt
  Util\Audio.txt
  Area\BossUtil.txt
  Util\Party.txt
  Util\CommonDialog.txt
  Util\VisualEffect.txt
End Reference

Procedure Main(startAt)

  Dim ac = Nothing

  RegisterCommonEventHandlers()
  AddAreaZone(AddZone("F1",  0,  0, 0,  31, 31, 100), 2)
  AddAreaZone(AddZone("F2", 32,  0, 0,  63, 31, 100), 8)
  AddAreaZone(AddZone("F3", 64,  0, 0,  95, 31, 100), 8)
  AddAreaZone(AddZone("F4", 96,  0, 0, 127, 31, 100), 8)
  AddAreaZone(AddZone("F5",  0, 32, 0,  31, 63, 100), 8)
  AddAreaZone(AddZone("F6", 32, 32, 0,  63, 63, 100), 8)
  AddAreaZone(AddZone("F7", 64, 32, 0,  95, 63, 100), 8)
  AddAreaZone(AddZone("B1", 96, 32, 0, 127, 63, 100), 1)

  AddZone("ExitArea", 15, 31, 0, 16, 31, 0)
  RegisterEventHandler("ExitAreaEnter", ExitArea_Enter)
  AddZone("Entrance", 15, 30, 0)

  AddZone("F1_DS1", 4, 25, 0)
  RegisterEventHandler("F1_DS1Enter", F1_DS1_Enter)
  AddZone("F1_US1", 5,  6, 0)
  RegisterEventHandler("F1_US1Enter", F1_US1_Enter)
  AddZone("F1_HL1", 15,  9, 0)
  RegisterEventHandler("F1_HL1Enter", F1_HL1_Enter)
  AddZone("F1_HL2", 22,  9, 0)
  RegisterEventHandler("F1_HL2Enter", F1_HL2_Enter)
  AddZone("F1_HL3", 25,  9, 0)
  RegisterEventHandler("F1_HL3Enter", F1_HL3_Enter)
  AddZone("F1_HL4", 15, 14, 0)
  RegisterEventHandler("F1_HL4Enter", F1_HL4_Enter)
  AddZone("F1_HL5", 22, 14, 0)
  RegisterEventHandler("F1_HL5Enter", F1_HL5_Enter)
  AddZone("F1_HL6", 27, 14, 0)
  RegisterEventHandler("F1_HL6Enter", F1_HL6_Enter)
  AddZone("F1_HL7", 17, 19, 0)
  RegisterEventHandler("F1_HL7Enter", F1_HL7_Enter)
  AddZone("F1_HL8", 20, 19, 0)
  RegisterEventHandler("F1_HL8Enter", F1_HL8_Enter)
  AddZone("F1_HL9", 25, 19, 0)
  RegisterEventHandler("F1_HL9Enter", F1_HL9_Enter)
  AddZone("F1_HL10",25, 24, 0)
  RegisterEventHandler("F1_HL10Enter", F1_HL10_Enter)

  AddZone("F2_DS1", 37, 6, 0)
  RegisterEventHandler("F2_DS1Enter", F2_DS1_Enter)
  AddZone("F2_US1", 58, 6, 0)
  RegisterEventHandler("F2_US1Enter", F2_US1_Enter)
  ac = AddActor("F2_SW1", 34, 29, 0, [A_FloorButtonOn], ActorMotion.Default)
  SetActorData(ac, "Enabled", True)
  RegisterEventHandler("F2_SW1Enter", F2_SW1_Enter)
  ac = AddActor("F2_SW2", 61,  4, 0, [A_FloorButtonOff], ActorMotion.Default)
  SetActorData(ac, "Enabled", False)
  RegisterEventHandler("F2_SW2Enter", F2_SW2_Enter)
  ac = AddActor("F2_WL2", 61, 29, 0, [A_Wall], ActorMotion.Default)
  ac = AddActor("F2_WL3", 39,  4, 0, [A_Wall], ActorMotion.Default)

  AddZone("F3_DS1", 90,  6, 0)
  RegisterEventHandler("F3_DS1Enter", F3_DS1_Enter)
  AddZone("F3_US1", 83, 24, 0)
  RegisterEventHandler("F3_US1Enter", F3_US1_Enter)
  AddZone("F3_US2", 88, 24, 0)
  RegisterEventHandler("F3_US2Enter", F3_US2_Enter)

  AddZone("F4_DS1", 115, 24, 0)
  RegisterEventHandler("F4_DS1Enter", F4_DS1_Enter)
  AddZone("F4_DS2", 120, 24, 0)
  RegisterEventHandler("F4_DS2Enter", F4_DS2_Enter)
  AddZone("F4_US1", 120,  8, 0)
  RegisterEventHandler("F4_US1Enter", F4_US1_Enter)
  ac = AddActor("F4_WL1", 100, 6, 0, [A_Wall], ActorMotion.Default)

  AddZone("F5_DS1", 24, 40, 0)
  RegisterEventHandler("F5_DS1Enter", F5_DS1_Enter)
  AddZone("F5_US1",  9, 42, 0)
  RegisterEventHandler("F5_US1Enter", F5_US1_Enter)
  AddZone("F5_US2", 12, 48, 0)
  RegisterEventHandler("F5_US2Enter", F5_US2_Enter)
  AddZone("F5_US3", 22, 48, 0)
  RegisterEventHandler("F5_US3Enter", F5_US3_Enter)

  AddZone("F6_DS1", 41, 42, 0)
  RegisterEventHandler("F6_DS1Enter", F6_DS1_Enter)
  AddZone("F6_DS2", 44, 48, 0)
  RegisterEventHandler("F6_DS2Enter", F6_DS2_Enter)
  AddZone("F6_DS3", 54, 48, 0)
  RegisterEventHandler("F6_DS3Enter", F6_DS3_Enter)
  AddZone("F6_US1", 47, 52, 0)
  RegisterEventHandler("F6_US1Enter", F6_US1_Enter)
  ac = AddActor("F6_SW1", 54, 41, 0, [A_FloorButtonOff], ActorMotion.Default)
  SetActorData(ac, "Enabled", False)
  RegisterEventHandler("F6_SW1Enter", F6_SW1_Enter)

  AddZone("F7_DS1", 79, 52, 0)
  RegisterEventHandler("F7_DS1Enter", F7_DS1_Enter)
  ac = AddActor("F7_SW1", 80, 44, 0, [A_FloorButtonOn], ActorMotion.Default)
  SetActorData(ac, "Enabled", True)
  RegisterEventHandler("F7_SW1Enter", F7_SW1_Enter)

  AddZone("B1_US1", 100, 57, 0)
  RegisterEventHandler("B1_US1Enter", B1_US1_Enter)
  AddZone("B1_HF1", 111, 41, 0)
  AddZone("B1_HF2", 118, 41, 0)
  AddZone("B1_HF3", 121, 41, 0)
  AddZone("B1_HF4", 111, 46, 0)
  AddZone("B1_HF5", 118, 46, 0)
  AddZone("B1_HF6", 123, 46, 0)
  AddZone("B1_HF7", 113, 51, 0)
  AddZone("B1_HF8", 116, 51, 0)
  AddZone("B1_HF9", 121, 51, 0)
  AddZone("B1_HF10", 121, 56, 0)

  Dim box_cb = Nothing
  If GetAliveBossCount() >= 3 Then
    box_cb = BoxStillSealed
  End If
  
  AddBox("WestTower_B1", 10, 5, 0, BoxContent.Money, 8700, box_cb)
  AddBox("WestTower_B2", 52, 6, 0, BoxContent.Money, 9300, box_cb)
  AddBox("WestTower_B3", 70, 7, 0, BoxContent.Tool, [BlackRing], Nothing)
  AddBox("WestTower_B4", 83, 7, 0, BoxContent.Tool, [BlackRobe], Nothing)
  AddBox("WestTower_B5", 116, 8, 0, BoxContent.Money, 9900, box_cb)
  AddBox("WestTower_B6", 17, 48, 0, BoxContent.Money, 10500, box_cb)
  AddBox("WestTower_B7", 7, 48, 0, BoxContent.Money, 11100, box_cb)
  AddBox("WestTower_B8", 51, 42, 0, BoxContent.Money, 11700, box_cb)
  
  If Not TestFlag("WestTower_1") Then
    ac = AddStaticActor("Boss", 76, 13, 0, [E303].Actor, Nothing)
  End If
  
  AddZone("BossCaution", 76, 21, 0, 77, 21, 0)
  RegisterEventHandler("BossCautionEnter", BossCaution_Enter)
  
  SetStepsOfMaterial(4, 20)
  ResetStepsForEncountering()
  
  AddZone("EncZone01", 0, 0, 0, 127, 127, 0)
  RegisterEventHandler("EncZone01Step", EncZone01_Step)

  DoInsideWarp(startAt, Direction.Up, False)
  
  If Not TestFlag("WestTower_1") Then
    ChangeBGM(Music.Tower)
  Else
    ChangeBGM(Music.Duel)
  End If

  NotifyEntered()
  Idle(ProcessInput)
  NotifyExiting()

End Procedure

Procedure ExitArea_Enter()
  BeginOutsideWarp([Field], GetAreaObject().Tag)
End Procedure

Procedure F1_DS1_Enter()
  DoStairWarp("B1_US1", Direction.Down, True)
End Procedure

Procedure F1_US1_Enter()
  DoStairWarp("F2_DS1", Direction.Down, True)
End Procedure

Procedure F1_HL1_Enter(the_bounds)
  DoFallingWarp(the_bounds, "B1_HF1", Direction.Down, True)
End Procedure

Procedure F1_HL2_Enter(the_bounds)
  DoFallingWarp(the_bounds, "B1_HF2", Direction.Down, True)
End Procedure

Procedure F1_HL3_Enter(the_bounds)
  DoFallingWarp(the_bounds, "B1_HF3", Direction.Down, True)
End Procedure

Procedure F1_HL4_Enter(the_bounds)
  DoFallingWarp(the_bounds, "B1_HF4", Direction.Down, True)
End Procedure

Procedure F1_HL5_Enter(the_bounds)
  DoFallingWarp(the_bounds, "B1_HF5", Direction.Down, True)
End Procedure

Procedure F1_HL6_Enter(the_bounds)
  DoFallingWarp(the_bounds, "B1_HF6", Direction.Down, True)
End Procedure

Procedure F1_HL7_Enter(the_bounds)
  DoFallingWarp(the_bounds, "B1_HF7", Direction.Down, True)
End Procedure

Procedure F1_HL8_Enter(the_bounds)
  DoFallingWarp(the_bounds, "B1_HF8", Direction.Down, True)
End Procedure

Procedure F1_HL9_Enter(the_bounds)
  DoFallingWarp(the_bounds, "B1_HF9", Direction.Down, True)
End Procedure

Procedure F1_HL10_Enter(the_bounds)
  DoFallingWarp(the_bounds, "B1_HF10", Direction.Down, True)
End Procedure

Procedure F2_DS1_Enter()
  DoStairWarp("F1_US1", Direction.Down, True)
End Procedure

Procedure F2_US1_Enter()
  DoStairWarp("F3_DS1", Direction.Down, True)
End Procedure

Procedure F2_SW1_Enter(the_actor)
  Dim F2_SW1 = GetActor(the_actor)
  If F2_SW1.Enabled Then
    AddActor("F2_WL1", 34,  4, 0, [A_Wall], ActorMotion.Default)
    RemoveActor("F2_WL3")
    F2_SW1.Material = [A_FloorButtonOff]
  Else
    AddActor("F2_WL3", 39,  4, 0, [A_Wall], ActorMotion.Default)
    RemoveActor("F2_WL1")
    F2_SW1.Material = [A_FloorButtonOn]
  End If
  F2_SW1.Enabled = Not F2_SW1.Enabled
End Procedure

Procedure F2_SW2_Enter(the_actor)
  Dim F2_SW2 = GetActor(the_actor)
  If F2_SW2.Enabled Then
    AddActor("F2_WL2", 61, 29, 0, [A_Wall], ActorMotion.Default)
    RemoveActor("F2_WL4")
    F2_SW2.Material = [A_FloorButtonOff]
  Else
    AddActor("F2_WL4", 39,  5, 0, [A_Wall], ActorMotion.Default)
    RemoveActor("F2_WL2")
    F2_SW2.Material = [A_FloorButtonOn]
  End If
  F2_SW2.Enabled = Not F2_SW2.Enabled
End Procedure

Procedure F3_DS1_Enter()
  DoStairWarp("F2_US1", Direction.Down, True)
End Procedure

Procedure F3_US1_Enter()
  DoStairWarp("F4_DS1", Direction.Down, True)
End Procedure

Procedure F3_US2_Enter()
  DoStairWarp("F4_DS2", Direction.Down, True)
End Procedure

Procedure F4_DS1_Enter()
  DoStairWarp("F3_US1", Direction.Down, True)
End Procedure

Procedure F4_DS2_Enter()
  DoStairWarp("F3_US2", Direction.Down, True)
End Procedure

Procedure F4_US1_Enter()
  DoStairWarp("F5_DS1", Direction.Down, True)
End Procedure

Procedure F5_DS1_Enter()
  DoStairWarp("F4_US1", Direction.Down, True)
End Procedure

Procedure F5_US1_Enter()
  DoStairWarp("F6_DS1", Direction.Down, True)
End Procedure

Procedure F5_US2_Enter()
  DoStairWarp("F6_DS2", Direction.Down, True)
End Procedure

Procedure F5_US3_Enter()
  DoStairWarp("F6_DS3", Direction.Down, True)
End Procedure

Procedure F6_DS1_Enter()
  DoStairWarp("F5_US1", Direction.Down, True)
End Procedure

Procedure F6_DS2_Enter()
  DoStairWarp("F5_US2", Direction.Down, True)
End Procedure

Procedure F6_DS3_Enter()
  DoStairWarp("F5_US3", Direction.Down, True)
End Procedure

Procedure F6_US1_Enter()
  DoStairWarp("F7_DS1", Direction.Down, True)
End Procedure

Procedure F6_SW1_Enter(the_actor)
  Dim F6_SW1 = GetActor(the_actor)
  If F6_SW1.Enabled Then
    RemoveActor("F7_WL1")
    F6_SW1.Material = [A_FloorButtonOff]
  Else
    AddActor("F7_WL1", 80, 48, 0, [A_Wall], ActorMotion.Default)
    F6_SW1.Material = [A_FloorButtonOn]
  End If
  F6_SW1.Enabled = Not F6_SW1.Enabled
  DoMsg("上のほうで何かが動いた")
End Procedure

Procedure F7_DS1_Enter()
  DoStairWarp("F6_US1", Direction.Down, True)
End Procedure

Procedure F7_SW1_Enter(the_actor)
  Dim F7_SW1 = GetActor(the_actor)
  If F7_SW1.Enabled Then
    RemoveActor("F4_WL1")
    F7_SW1.Material = [A_FloorButtonOff]
  Else
    AddActor("F4_WL1", 100, 6, 0, [A_Wall], ActorMotion.Default)
    F7_SW1.Material = [A_FloorButtonOn]
  End If
  F7_SW1.Enabled = Not F7_SW1.Enabled
  DoMsg("ずっと下のほうで何かが動いた")
End Procedure

Procedure B1_US1_Enter()
  DoStairWarp("F1_DS1", Direction.Down, True)
End Procedure

Procedure EncZone01_Step(mat)
  If Not TestFlag("WestTower_1") Then
    If TestEncountering(mat) Then
      DoNormalBattle(GetEnemyPartyList())
      ResetStepsForEncountering()
    End If
  End If
End Procedure

Procedure BossCaution_Enter()
  Dim won_this_duel = False
  Dim p_name = [TheHero].Name + "："
  Dim q_name = "アフリエム："
  Dim has_ruby = AnybodyHas([RoyalRuby])
  If TestFlag("WestTower_1") Then Return
  DoMsg(p_name, "（焼けるような熱気を感じる……）")
  If DoYesNoMsg("この先に進みますか？") = True Then
    For i = 1 To 6
      LetActorWalk(GetPrimaryActor(), Direction.Up)
    Next
    ChangeBGM(Nothing)
    Sleep(1000)
    If TestFlag("SouthTower_1") Then
      ChangeBGM(Music.Duel)
      DoMsg(q_name, "貴様……" + [TheHero].Name + "だな？")
      DoMsg(p_name, "そうだ　お前がアフリエムか")
      DoMsg(q_name, "我が弟　フラスタンの仇　取らせてもらうぞ")
    ElseIf TestFlag("NorthTower_1") Then
      ChangeBGM(Music.Duel)
      DoMsg(q_name, "貴様……" + [TheHero].Name + "か？")
      DoMsg(p_name, "そうだ")
      DoMsg(q_name, "俺はアフリエム　魔族四天王の一人だ")
      Sleep(500)
      DoMsg(q_name, "あのセモサーがやられたと言うから", _
        "どんな奴かと思っていたが……こんな小僧か！", _
        "あいつも衰えたな")
      DoMsg(p_name, "次はお前の番だ　アフリエム", _
        "邪神復活なんて　させないぞ")
      DoMsg(q_name, "ふん　まあ少しは力があるんだろう", _
        "だが貴様もここまでだ", _
        "我が炎で燃えぬものは無い")
    ElseIf TestFlag("EastTower_1") Then
      ChangeBGM(Music.Duel)
      DoMsg(q_name, "貴様……" + [TheHero].Name + "か？")
      DoMsg(p_name, "そうだ")
      DoMsg(q_name, "俺はアフリエム　魔族四天王の一人だ")
      Sleep(500)
      DoMsg(q_name, "アルタロスがやられたと言うから", _
        "どんな奴かと思っていたが……こんな小僧か！", _
        "あいつの闘気とやらも　大したことはないな")
      DoMsg(p_name, "次はお前の番だ　アフリエム", _
        "邪神復活なんて　させないぞ")
      DoMsg(q_name, "ふん", _
        "アルタロスを倒したくらいでいい気になるな", _
        "我が炎で燃えぬものは無い")
    Else
      DoMsg(q_name, "ここまで来られる人間がいるとは……")
      DoMsg(p_name, "お前は？……")
      DoMsg(q_name, "俺はアフリエム　魔族四天王の一人だ")
      Sleep(500)
      ChangeBGM(Music.Duel)
      DoMsg(q_name, "我々の計画を阻止しようなどと無駄なこと", _
        "わざわざ死に急ぐ必要もあるまい")
      DoMsg(p_name, "なんだと？")
      DoMsg(q_name, "今　俺以外の３人も　各地の塔から", _
        "デロク様を甦らせるべく精神波を送っている", _
        "", _
        "貴様のせいで少し遅れが出たが", _
        "復活はさほど先の話ではないだろう", _
        "そうすれば邪魔な人間どもは滅び", _
        "地上は我々魔族のものとなるのだ")
      DoMsg(p_name, "そういう事だったのか……", _
        "だが　そんな事はさせないぞ", _
        "お前を倒し　計画を阻止してやる　覚悟しろ")
      DoMsg(q_name, "なに　本気で戦うつもりか？", _
        "俺も見くびられたものだ")
    End If
    AlphaFadeInProc(192, 237, 26, 61, 2000)
    DoMsg(q_name, "灰になって死ね！")
    If has_ruby Then
      [TheHero].DamageDP[2] += 180
    End If
    If Not TestFlag("WestEvo") Then
      won_this_duel = DoBossBattle({[E303]})
    Else
      won_this_duel = DoBossBattle({[E307]})
    End If
    AlphaFadeOutProc(192, 237, 26, 61, 0)
    If Not won_this_duel Then
      EndOfTheWorld()
      Return
    End If
    If has_ruby Then
      [TheHero].DamageDP[2] -= 180
      SetFlag("RRSpoiled")
    End If
    If TestFlag("SouthTower_1") Then
      DoMsg(q_name, "お　弟よ　すまぬ……", _
        "仇は　取れなんだ……")
    Else
      DoMsg(q_name, "ば　ばかな……", _
        "この俺が　人間如きに……")
      Sleep(500)
      DoMsg(q_name, "だが　貴様も　朱雀の塔で　死ぬのだ", _
        "我が弟　フラスタンの　冷気で　氷となってな……")
    End If
    Sleep(500)
    DoMsg(q_name, "ぐふっ")
    RemoveActor(GetActor("Boss"))
    SetFlag("WestTower_1")
    EvoBoss()
    If Not TestFlag("SouthTower_1") Then SetFlag("SouthEvo")
  Else
    LetActorWalk(GetPrimaryActor(), Direction.Down)
  End If
End Procedure
